window.onload=function () {
    const E = window.wangEditor
    const editor = new E('#div1')
    const text = $('#text')

    // 监控变化，同步更新到 textarea
    editor.config.onchange = function (html) {
        text.val(html)
    }
    //设置上传的参数名
    editor.config.uploadFileName = 'file'
    // 配置 server 接口地址
    editor.config.uploadImgServer = 'upload-img.do';
    // 配置alt选项
    editor.config.showLinkImgAlt = false;
    // 配置超链接
    editor.config.showLinkImgHref = false;
    //去除全屏
    editor.config.showFullScreen = false;
    editor.config.uploadImgTimeout = 20 * 1000;
    editor.config.height = 500;
    //配置菜单
    editor.config.menus = [
        'head',
        'bold',
        'fontSize',
        'fontName',
        'italic',
        'underline',
        'strikeThrough',
        'indent',
        'lineHeight',
        'foreColor',
        'backColor',
        'link',
        'list',
        'todo',
        'justify',
        'quote',
        'emoticon',
        'image',
        'code',
        'splitLine',
        'undo',
        'redo',
    ]

    editor.create();
    //将获取到的内容放入Editor中
    editor.txt.html($("#posttext").val());
    //清除posttext
    $("#posttext").remove();

    //动态获取帖子类型下拉框的选项

    //获取当前选择下拉框中的所有值
    var vals = new Array();
    // var texts = new Array();
    function newBuildReturnValue(selectName){
        $("#"+selectName+" option").each(function(){
            //遍历所有option
            var value = $(this).val();   //获取option值
            // var text = $(this).text();
            vals.push(value);
            // texts.push("2");
        });
    }
    newBuildReturnValue("selectType");

    // $.ajax({
    //     type:"get",
    //     url:"viewPost.do",
    //     dataType:"text",
    //     data:{pid:args.pid},
    //     success:function(back){
    //         console.log(back);
    //         var obj = eval( "(" + back + ")" );//转换后的JSON对象
    //         //下拉框选择后端传过来对应的选项
    //         $("#selectType option[value='"+obj.post.ptype+"']").attr("selected","selected");
    //         document.getElementById("title").innerHTML += obj.post.ptitle;
    //         // 将获取到的内容放入Editor中
    //         editor.txt.html(obj.post.pcontent);
    //         //修改页面title
    //         $(document).attr('title',obj.ptitle);
    //     },
    //     error:function(){
    //         console.log("请求访问帖子数据失败");
    //     }
    // });

    //获取当前时间
    function getCurrentTime(){
        let now= new Date();
        let _month = ( 10 > (now.getMonth()+1) ) ? '0' + (now.getMonth()+1) : now.getMonth()+1;
        let _day = ( 10 > now.getDate() ) ? '0' + now.getDate() : now.getDate();
        let _hour = ( 10 > now.getHours() ) ? '0' + now.getHours() : now.getHours();
        let _minute = ( 10 > now.getMinutes() ) ? '0' + now.getMinutes() : now.getMinutes();
        let _second = ( 10 > now.getSeconds() ) ? '0' + now.getSeconds() : now.getSeconds();
        return now.getFullYear() + '-' + _month + '-' + _day + ' ' + _hour + ':' + _minute + ':' + _second;
    }
    //获取当前日期
    function getCurrentDate(){
        let now= new Date();
        let _month = ( 10 > (now.getMonth()+1) ) ? '0' + (now.getMonth()+1) : now.getMonth()+1;
        let _day = ( 10 > now.getDate() ) ? '0' + now.getDate() : now.getDate();
        return now.getFullYear() + '-' + _month + '-' + _day;
    }
    //判断输入框是否为空格
    function judgeTrim(str) {
        return str.replace(/(^\s*)|(\s*$)/g, "");
    }
    //发布帖子
    $("#submit").click(function () {
        var tid = $("#selectType").val();
        var ptitle = $("#title").val();
        var pcontent = $("#text").val();
        var title2 = judgeTrim(ptitle);
        var content2 = judgeTrim(pcontent);
        var topcheck = $(".topCheck");
        if (topcheck[0].checked==true){
            pstatus=2
        }else{
            pstatus=1
        }
        if (title2==""){
            alert("标题输入有误");
            return;
        }
        if (content2==""){
            alert("内容有误")
        }else {
            var ptime = getCurrentTime();
            $.post('updatePost.do', {
                pid: args.pid,
                tid: tid,
                ptitle: ptitle,
                pcontent: pcontent,
                ptime: ptime,
                pstatus: pstatus,
                ldate:getCurrentDate()
            }, function (back) {
                // $("#title").val("");
                // text清空之后 点击回退重新获取的帖子内容会再次读取text就又有值了
                // $("#text").val("");
                window.location.href = '/html/editor/viewPost.do?pid=' + args.pid;
                // console.log(back);
            });
        }
    });
    //另存为草稿
    $("#draft1").click(function () {
        var type = $("#selectType").val();
        var title = $("#title").val();
        var content = $("#text").val();
        var title2 = judgeTrim(title);
        var content2 = judgeTrim(content);
        if (title2==""){
            alert("标题输入有误");
            return;
        }
        if (content2==""){
            alert("内容有误")
        }else {
            var time = getCurrentTime();
            $.post('publishPost.do', {
                tid: type,
                title: title,
                content: content,
                time: time,
                pstatus: 0,
                ldate:getCurrentDate()
            }, function (back) {
                window.location.href = '/html/editor/viewPost.do?pid=' + back[0];
                // console.log(pid);
            });
        }
    });

    //撤销发布重新保存为草稿
    $("#draft2").click(function () {
        var tid = $("#selectType").val();
        var ptitle = $("#title").val();
        var pcontent = $("#text").val();
        var title2 = judgeTrim(ptitle);
        var content2 = judgeTrim(pcontent);
        if (title2==""){
            alert("标题输入有误");
            return;
        }
        if (content2==""){
            alert("内容有误")
        }else {
            var ptime = getCurrentTime();
            $.post('updatePost.do', {
                pid: args.pid,
                tid: tid,
                ptitle: ptitle,
                pcontent: pcontent,
                ptime: ptime,
                pstatus: 0,
                ldate:getCurrentDate()
            }, function (back) {
                window.location.href = '/html/editor/viewPost.do?pid=' + args.pid;
                // console.log(back);
            });
        }
    });
}